CMAKE_MINIMUM_REQUIRED(VERSION 2.6 FATAL_ERROR)
PROJECT(Server2)
LIST(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)
 
SET(CMAKE_CXX_COMPILER "g++")
IF(WIN32) 
    SET(CMAKE_FIND_LIBRARY_SUFFIXES ".lib" ".dll" ${CMAKE_FIND_LIBRARY_SUFFIXES})
    SET(CMAKE_FIND_LIBRARY_PREFIXES "" "lib") 
    SET(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} ${CMAKE_SOURCE_DIR}/lib/include ${CMAKE_SOURCE_DIR}/lib/include/log4cxx ${CMAKE_SOURCE_DIR}/lib/include/tinyxml)
    SET(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} ${CMAKE_SOURCE_DIR}/lib/lib)
ELSEIF(UNIX)
    SET(CMAKE_FIND_LIBRARY_SUFFIXES ".a" ".so" ".la" ${CMAKE_FIND_LIBRARY_SUFFIXES})
    SET(CMAKE_FIND_LIBRARY_PREFIXES "" "lib")
    SET(CMAKE_INCLUDE_PATH /usr/include ${CMAKE_INCLUDE_PATH})
    SET(CMAKE_LIBRARY_PATH /usr/lib /usr/lib64 ${CMAKE_LIBRARY_PATH})
ENDIF(WIN32)

INCLUDE(HashMapSupport)
# - Try to find precompiled headers support for GCC 3.4 and 4.x
# Once done this will define:
#
# Variable:
#   PCHSupport_FOUND
#
# Macro:
#   ADD_PRECOMPILED_HEADER  _targetName _input  _dowarn
#   ADD_PRECOMPILED_HEADER_TO_TARGET _targetName _input _pch_output_to_use _dowarn
#   ADD_NATIVE_PRECOMPILED_HEADER _targetName _input _dowarn
#   GET_NATIVE_PRECOMPILED_HEADER _targetName _input
##INCLUDE(PCHSupport_26)
#
# Find the MySQL client includes and library
#

# This module defines
# MYSQL_INCLUDE_DIRECTORIES, where to find mysql.h
# MYSQL_LIBRARIES, the libraries to link against to connect to MySQL
# MYSQL_FOUND, If false, you cannot build anything that requires MySQL.

# also defined, but not for general use are
# MYSQL_LIBRARY, where to find the MySQL library.

#
# XXX NOTE: This is not yet for general use.  I'm pretty sure there
# are other libraries I have to link against at the same time.
#
FIND_PACKAGE(MySQL)

# Finds the International Components for Unicode (ICU) Library
#
#  ICU_FOUND          - True if ICU found.
#  ICU_I18N_FOUND     - True if ICU's internationalization library found.
#  ICU_INCLUDE_DIRS   - Directory to include to get ICU headers
#                       Note: always include ICU headers as, e.g., 
#                       unicode/utypes.h
#  ICU_LIBRARIES      - Libraries to link against for the common ICU
#  ICU_I18N_LIBRARIES - Libraries to link against for ICU internationaliation
#                       (note: in addition to ICU_LIBRARIES)
#FIND_PACKAGE(ICU)

#FIND_PACKAGE(ZLIB)
# - Try to find the liblog4cxx libraries
# Once done this will define
#
# Log4cxx_FOUND - system has liblog4cxx
# LOG4CXX_INCLUDE_DIR - the liblog4cxx include directory
# LOG4CXX_LIBRARIES - liblog4cxx library
FIND_PACKAGE(Log4cxx)
# - Find Kfs
# Find the native Kfs includes and library
#
#  Kfs_INCLUDE_DIR - where to find Kfs.h, etc.
#  Kfs_LIBRARIES   - List of libraries when using Kfs.
#  Kfs_FOUND       - True if Kfs found.

# - Find Log4cpp
# Find the native Log4cpp includes and library
#
#  Log4cpp_INCLUDE_DIR - where to find Log4cpp.h, etc.
#  Log4cpp_LIBRARIES   - List of libraries when using Log4cpp.
#  Log4cpp_FOUND       - True if Log4cpp found.
SET(CMAKE_VERBOSE_MAKEFILE ON)
SET(Boost_USE_STATIC_LIBS ON)
SET(Boost_USE_MULTITHREAD ON)
# Variables used by this module, they can change the default behaviour and need to be set
# before calling find_package:
#  Boost_USE_MULTITHREAD         Can be set to OFF to use the non-multithreaded
#                                boost libraries. Defaults to ON.
#  Boost_USE_STATIC_LIBS         Can be set to ON to force the use of the static
#                                boost libraries. Defaults to OFF.
#  Boost_ADDITIONAL_VERSIONS     A list of version numbers to use for searching
#                                the boost include directory. The default list
#                                of version numbers is:
#                                1.33, 1.33.0, 1.33.1, 1.34, 1.34.0, 1.34.1, 
#                                1.35, 1.35.0, 1.35.1, 1.36, 1.36.0, 1.36.1
#                                If you want to look for an older or newer
#                                version set this variable to a list of
#                                strings, where each string contains a number, i.e.
#                                SET(Boost_ADDITIONAL_VERSIONS "0.99.0" "1.35.0")
#  BOOST_ROOT or BOOSTROOT       Preferred installation prefix for searching for Boost,
#                                set this if the module has problems finding the proper Boost installation
#  BOOST_INCLUDEDIR              Set this to the include directory of Boost, if the
#                                module has problems finding the proper Boost installation
#  BOOST_LIBRARYDIR              Set this to the lib directory of Boost, if the
#                                module has problems finding the proper Boost installation
#
#  The last three variables are available also as environment variables
#
#
# Variables defined by this module:
#
#  Boost_FOUND                          System has Boost, this means the include dir was found,
#                                       as well as all the libraries specified in the COMPONENTS list
#  Boost_INCLUDE_DIRS                   Boost include directories, not cached
#  Boost_INCLUDE_DIR                    This is almost the same as above, but this one is cached and may be
#                                       modified by advanced users
#  Boost_LIBRARIES                      Link these to use the Boost libraries that you specified, not cached
#  Boost_LIBRARY_DIRS                   The path to where the Boost library files are.
#  Boost_VERSION                        The version number of the boost libraries that have been found,
#                                       same as in version.hpp from Boost
#  Boost_LIB_VERSION                    The version number in filename form as its appended to the library filenames
#  Boost_MAJOR_VERSION                  major version number of boost
#  Boost_MINOR_VERSION                  minor version number of boost
#  Boost_SUBMINOR_VERSION               subminor version number of boost
#  Boost_LIB_DIAGNOSTIC_DEFINITIONS     Only set on windows. Can be used with add_definitions 
#                                       to print diagnostic information about the automatic 
#                                       linking done on windows.

# For each component you list the following variables are set.
# ATTENTION: The component names need to be in lower case, just as the boost
# library names however the cmake variables use upper case for the component
# part. So you'd get Boost_SERIALIZATION_FOUND for example.
#
#  Boost_${COMPONENT}_FOUND             True IF the Boost library "component" was found.
#  Boost_${COMPONENT}_LIBRARY           The absolute path of the Boost library "component".
#  Boost_${COMPONENT}_LIBRARY_DEBUG     The absolute path of the debug version of the
#                                       Boost library "component".
#  Boost_${COMPONENT}_LIBRARY_RELEASE   The absolute path of the release version of the
#                                       Boost library "component"
FIND_PACKAGE(Boost 1.35.0 REQUIRED COMPONENTS system thread date_time regex filesystem unit_test_framework program_options)
IF(Boost_FOUND)
    MESSAGE("Found boost\n")
    INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
    MESSAGE(STATUS ${Boost_LIBRARIES})
ENDIF()

FIND_PACKAGE(Threads REQUIRED)

INCLUDE(CheckIncludeFiles)
INCLUDE(CheckSymbolExists)
INCLUDE(CheckFunctionExists)
INCLUDE(CheckTypeSize)

CHECK_INCLUDE_FILES (malloc.h HAVE_MALLOC_H)
CHECK_INCLUDE_FILES ("arpa/inet.h" HAVE_INET_H)
CHECK_INCLUDE_FILES ("WinSock2.h" HAVE_WINSOCK2_H) 

IF(HAVE_INET_H)
    CHECK_FUNCTION_EXISTS(ntohl HAVE_NTOHL)
    CHECK_FUNCTION_EXISTS(ntohs HAVE_NTOHS)
    CHECK_FUNCTION_EXISTS(htonl HAVE_HTONL)
    CHECK_FUNCTION_EXISTS(htons HAVE_HTONS)
ELSEIF(HAVE_WINSOCK2_H)
    CHECK_FUNCTION_EXISTS(ntohl HAVE_NTOHL)
    CHECK_FUNCTION_EXISTS(ntohs HAVE_NTOHS)
    CHECK_FUNCTION_EXISTS(htonl HAVE_HTONL)
    CHECK_FUNCTION_EXISTS(htons HAVE_HTONS)
ENDIF()

CHECK_INCLUDE_FILES("sys/epoll.h" HAVE_EPOLL)
CHECK_INCLUDE_FILES (dlfcn.h HAVE_DL)
CHECK_INCLUDE_FILES (winbase.h HAVE_WINBASE)

check_type_size("int" TL_BYTES_PER_INT BUILDIN_TYPES_ONLY)
check_type_size("short" TL_BYTES_PER_SHORT BUILDIN_TYPES_ONLY)
check_type_size("long" TL_BYTES_PER_LONG BUILDIN_TYPES_ONLY)
check_type_size("long long" HAVE_LONG_LONG BUILDIN_TYPES_ONLY)
check_type_size("char" TL_BYTES_PER_BYTE BUILDIN_TYPES_ONLY)
check_type_size("unsigned" TL_BYTES_PER_UNSIGNED BUILDIN_TYPES_ONLY)

INCLUDE(TestBigEndian)
TEST_BIG_ENDIAN(IS_BIG_ENDIAN)
IF(NOT IS_BIG_ENDIAN)
    SET(IS_LITTLE_ENDIAN 1)
ELSE(NOT IS_BIG_ENDIAN)
    SET(IS_BIG_ENDIAN 1)
ENDIF(NOT IS_BIG_ENDIAN)

FIND_PACKAGE(OpenMP)

MESSAGE(STATUS "************************************")
MESSAGE(STATUS "System compiler under " ${CMAKE_SYSTEM_PROCESSOR})
MESSAGE(STATUS "************************************\n")
SET(PLATFORM_PROCESSOR_TRAIT ${CMAKE_SYSTEM_PROCESSOR})
IF(CMAKE_SYSTEM_PROCESSOR MATCHES "64")
    SET(IS_X86_64 1)
ELSE(CMAKE_SYSTEM_PROCESSOR MATCHES "64")
    SET(IS_X86_32 1)
ENDIF(CMAKE_SYSTEM_PROCESSOR MATCHES "64")
IF(WIN32)
    SET(IS_WIN32 1)
    IF(MSVC)
        SET(IS_MSVC 1)
        IF(MSVC60)
            SET(IS_MSVC60 1)
        ENDIF(MSVC60)
        IF(MSVC70)
            SET(IS_MSVC70 1)
        ENDIF(MSVC70)
        IF(MSVC71)
            SET(IS_MSVC71 1)
        ENDIF(MSVC71)
        IF(MSVC80)
            SET(IS_MSVC80 1)
        ENDIF(MSVC80)
        IF(MSVC90)
            SET(IS_MSVC90 1)
        ENDIF(MSVC90)
    ENDIF(MSVC)
    IF(BORLAND)
        SET(IS_BORLAND 1)
    ENDIF(BORLAND)
    IF(CYGWIN)
        SET(IS_CYGWIN 1)
    ENDIF(CYGWIN)
    IF(MSYS)
        SET(IS_MSYS 1)
    ENDIF(MSYS)
    IF(MINGW)
        SET(IS_MINGW 1)
    ENDIF(MINGW)
    IF(WATCOM)
        SET(IS_WATCOM 1)
    ENDIF(WATCOM)
ELSEIF(UNIX)
    SET(IS_UNIX 1)
    IF(CMAKE_SYSTEM MATCHES "Linux")
        MESSAGE(STATUS ${CMAKE_SYSTEM})
        SET(IS_LINUX 1)
    ENDIF(CMAKE_SYSTEM MATCHES "Linux")
    IF(APPLE)
        SET(IS_APPLE 1)
    ENDIF(APPLE)
    IF(CMAKE_COMPILER_IS_GNUCC)
        SET(IS_GCC 1)
        IF(CMAKE_COMPILER_IS_GNUCXX)
            SET(IS_GXX 1)
            SET(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g -pg -fno-inline -rdynamic -Wno-deprecated -ftemplate-depth-50 -fPIC")
            SET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O2 -Wall -Wno-deprecated -ftemplate-depth-50 -fPIC")
        ENDIF(CMAKE_COMPILER_IS_GNUCXX)
    ENDIF(CMAKE_COMPILER_IS_GNUCC)
ELSE(WIN32)
    IF(CMAKE_SYSTEM MATCHES "Windows" AND IS_X86_64)
        SET(IS_WIN64 1)
    ENDIF(CMAKE_SYSTEM MATCHES "Windows" AND IS_X86_64)
ENDIF(WIN32)
MESSAGE(STATUS "Current environment variable $PATH is " $ENV{PATH})

# Find TinyXML Macros
FIND_PACKAGE(TinyXML REQUIRED)
IF(TL_TINYXML_FOUND)
    MESSAGE(STATUS "Found tinyxml success")
ELSE(TL_TINYXML_FOUND)
    MESSAGE(STATUS "Found tinyxml failed")
ENDIF(TL_TINYXML_FOUND)
MESSAGE(STATUS ${TL_TINYXML_INCLUDE_DIR})

CHECK_HASHMAP()
CONFIGURE_FILE(${PROJECT_SOURCE_DIR}/include/config.h.in ${PROJECT_SOURCE_DIR}/include/tlpr/tlcpucfg.h)
INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/include)
# ------------------------- Begin Generic CMake Variable Logging ------------------

# /*    C++ comment style not allowed   */


# if you are building in-source, this is the same as CMAKE_SOURCE_DIR, otherwise 
# this is the top level directory of your build tree 
MESSAGE( STATUS "CMAKE_BINARY_DIR:         " ${CMAKE_BINARY_DIR} )

# if you are building in-source, this is the same as CMAKE_CURRENT_SOURCE_DIR, otherwise this 
# is the directory where the compiled or generated files from the current CMakeLists.txt will go to 
MESSAGE( STATUS "CMAKE_CURRENT_BINARY_DIR: " ${CMAKE_CURRENT_BINARY_DIR} )

# this is the directory, from which cmake was started, i.e. the top level source directory 
MESSAGE( STATUS "CMAKE_SOURCE_DIR:         " ${CMAKE_SOURCE_DIR} )

# this is the directory where the currently processed CMakeLists.txt is located in 
MESSAGE( STATUS "CMAKE_CURRENT_SOURCE_DIR: " ${CMAKE_CURRENT_SOURCE_DIR} )

# contains the full path to the top level directory of your build tree 
MESSAGE( STATUS "PROJECT_BINARY_DIR: " ${PROJECT_BINARY_DIR} )

# contains the full path to the root of your project source directory,
# i.e. to the nearest directory where CMakeLists.txt contains the PROJECT() command 
MESSAGE( STATUS "PROJECT_SOURCE_DIR: " ${PROJECT_SOURCE_DIR} )

# set this variable to specify a common place where CMake should put all executable files
# (instead of CMAKE_CURRENT_BINARY_DIR)
MESSAGE( STATUS "EXECUTABLE_OUTPUT_PATH: " ${EXECUTABLE_OUTPUT_PATH} )

# set this variable to specify a common place where CMake should put all libraries 
# (instead of CMAKE_CURRENT_BINARY_DIR)
MESSAGE( STATUS "LIBRARY_OUTPUT_PATH:     " ${LIBRARY_OUTPUT_PATH} )

# tell CMake to search first in directories listed in CMAKE_MODULE_PATH
# when you use FIND_PACKAGE() or INCLUDE()
MESSAGE( STATUS "CMAKE_MODULE_PATH: " ${CMAKE_MODULE_PATH} )

# this is the complete path of the cmake which runs currently (e.g. /usr/local/bin/cmake) 
MESSAGE( STATUS "CMAKE_COMMAND: " ${CMAKE_COMMAND} )

# this is the CMake installation directory 
MESSAGE( STATUS "CMAKE_ROOT: " ${CMAKE_ROOT} )

# this is the filename including the complete path of the file where this variable is used. 
MESSAGE( STATUS "CMAKE_CURRENT_LIST_FILE: " ${CMAKE_CURRENT_LIST_FILE} )

# this is linenumber where the variable is used
MESSAGE( STATUS "CMAKE_CURRENT_LIST_LINE: " ${CMAKE_CURRENT_LIST_LINE} )

# this is used when searching for include files e.g. using the FIND_PATH() command.
MESSAGE( STATUS "CMAKE_INCLUDE_PATH: " ${CMAKE_INCLUDE_PATH} )

# this is used when searching for libraries e.g. using the FIND_LIBRARY() command.
MESSAGE( STATUS "CMAKE_LIBRARY_PATH: " ${CMAKE_LIBRARY_PATH} )

# the complete system name, e.g. "Linux-2.4.22", "FreeBSD-5.4-RELEASE" or "Windows 5.1" 
MESSAGE( STATUS "CMAKE_SYSTEM: " ${CMAKE_SYSTEM} )

# the short system name, e.g. "Linux", "FreeBSD" or "Windows"
MESSAGE( STATUS "CMAKE_SYSTEM_NAME: " ${CMAKE_SYSTEM_NAME} )

# only the version part of CMAKE_SYSTEM 
MESSAGE( STATUS "CMAKE_SYSTEM_VERSION: " ${CMAKE_SYSTEM_VERSION} )

# the processor name (e.g. "Intel(R) Pentium(R) M processor 2.00GHz") 
MESSAGE( STATUS "CMAKE_SYSTEM_PROCESSOR: " ${CMAKE_SYSTEM_PROCESSOR} )

# is TRUE on all UNIX-like OS's, including Apple OS X and CygWin
MESSAGE( STATUS "UNIX: " ${UNIX} )

# is TRUE on Windows, including CygWin 
MESSAGE( STATUS "WIN32: " ${WIN32} )

# is TRUE on Apple OS X
MESSAGE( STATUS "APPLE: " ${APPLE} )

# is TRUE when using the MinGW compiler in Windows
MESSAGE( STATUS "MINGW: " ${MINGW} )

# is TRUE on Windows when using the CygWin version of cmake
MESSAGE( STATUS "CYGWIN: " ${CYGWIN} )

# is TRUE on Windows when using a Borland compiler 
MESSAGE( STATUS "BORLAND: " ${BORLAND} )

# Microsoft compiler 
MESSAGE( STATUS "MSVC: " ${MSVC} )
MESSAGE( STATUS "MSVC_IDE: " ${MSVC_IDE} )
MESSAGE( STATUS "MSVC60: " ${MSVC60} )
MESSAGE( STATUS "MSVC70: " ${MSVC70} )
MESSAGE( STATUS "MSVC71: " ${MSVC71} )
MESSAGE( STATUS "MSVC80: " ${MSVC80} )
MESSAGE( STATUS "CMAKE_COMPILER_2005: " ${CMAKE_COMPILER_2005} )


# set this to true if you don't want to rebuild the object files if the rules have changed, 
# but not the actual source files or headers (e.g. if you changed the some compiler switches) 
MESSAGE( STATUS "CMAKE_SKIP_RULE_DEPENDENCY: " ${CMAKE_SKIP_RULE_DEPENDENCY} )

# since CMake 2.1 the install rule depends on all, i.e. everything will be built before installing. 
# If you don't like this, set this one to true.
MESSAGE( STATUS "CMAKE_SKIP_INSTALL_ALL_DEPENDENCY: " ${CMAKE_SKIP_INSTALL_ALL_DEPENDENCY} )

# If set, runtime paths are not added when using shared libraries. Default it is set to OFF
MESSAGE( STATUS "CMAKE_SKIP_RPATH: " ${CMAKE_SKIP_RPATH} )

# set this to true if you are using makefiles and want to see the full compile and link 
# commands instead of only the shortened ones 
MESSAGE( STATUS "CMAKE_VERBOSE_MAKEFILE: " ${CMAKE_VERBOSE_MAKEFILE} )

# this will cause CMake to not put in the rules that re-run CMake. This might be useful if 
# you want to use the generated build files on another machine. 
MESSAGE( STATUS "CMAKE_SUPPRESS_REGENERATION: " ${CMAKE_SUPPRESS_REGENERATION} )


# A simple way to get switches to the compiler is to use ADD_DEFINITIONS(). 
# But there are also two variables exactly for this purpose: 

# the compiler flags for compiling C sources 
MESSAGE( STATUS "CMAKE_C_FLAGS: " ${CMAKE_C_FLAGS} )

# the compiler flags for compiling C++ sources 
MESSAGE( STATUS "CMAKE_CXX_FLAGS: " ${CMAKE_CXX_FLAGS} )


# Choose the type of build.  Example: SET(CMAKE_BUILD_TYPE Debug) 
MESSAGE( STATUS "CMAKE_BUILD_TYPE: " ${CMAKE_BUILD_TYPE} )

# if this is set to ON, then all libraries are built as shared libraries by default.
MESSAGE( STATUS "BUILD_SHARED_LIBS: " ${BUILD_SHARED_LIBS} )

MESSAGE( STATUS "LINK_FLAGS: " ${LINK_FLAGS} )

# the compiler used for C files 
MESSAGE( STATUS "CMAKE_C_COMPILER: " ${CMAKE_C_COMPILER} )

# the compiler used for C++ files 
MESSAGE( STATUS "CMAKE_CXX_COMPILER: " ${CMAKE_CXX_COMPILER} )

# if the compiler is a variant of gcc, this should be set to 1 
MESSAGE( STATUS "CMAKE_COMPILER_IS_GNUCC: " ${CMAKE_COMPILER_IS_GNUCC} )

# if the compiler is a variant of g++, this should be set to 1 
MESSAGE( STATUS "CMAKE_COMPILER_IS_GNUCXX : " ${CMAKE_COMPILER_IS_GNUCXX} )

# the tools for creating libraries 
MESSAGE( STATUS "CMAKE_AR: " ${CMAKE_AR} )
MESSAGE( STATUS "CMAKE_RANLIB: " ${CMAKE_RANLIB} )

MESSAGE( STATUS "CMAKE_INCLUDE_PATH: " ${CMAKE_INCLUDE_PATH} )
MESSAGE( STATUS "CMAKE_LIBRARY_PATH: " ${CMAKE_LIBRARY_PATH} )
MESSAGE( STATUS "PATH: " $ENV{PATH} )

#
#MESSAGE( STATUS ": " ${} )

# ------------------------- End of Generic CMake Variable Logging ------------------
ADD_SUBDIRECTORY(${PROJECT_SOURCE_DIR}/src)
